package com.hemaapp.yjnh.activity;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.bean.User;
import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;

/**通用的网页界面
 * @author GuiyouZhang
 * Date：2016年2月19日 下午1:29:27
 * Email:guiyouzhang@163.com
 */
public class WebviewActivity extends BaseActivity implements OnClickListener {

	private ImageButton left;
	private TextView title;
	private Button right;
	private WebView webview;
	ProgressBar progressBar;
	private LinearLayout ll_coupons_info;

	private User user;
	private String path = "";
	private int type = 0;//1商品质量保证,2商品退货说明,3关于我们,4返利说明,5快递查询
	//6扫一扫的网页,7首页广告图文详情，8模块头部图文详情，9易家推荐图文详情, 10众农海报, 11 兔子等级说明
	//12其他情况，直接跳转到指定网址,13田家的申请说明,14田家介绍，15新手指南，16粮库分类广告图文详情
	//17返利、利率说明
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_aboutus);
		super.onCreate(savedInstanceState);
		user = getApplicationContext().getUser(); 
		initWebView();
		String sys_web_service = getApplicationContext().getSysInitInfo()
				.getSys_web_service();
		String website = "";
		if (type == 3 || type == 10 || type == 11 || type == 13 || type == 14 || type == 17) {
			website = sys_web_service + "webview/parm/"+path;
		}else if (type == 1) {
			website = sys_web_service + "webview/parm/blogquality_"+path;
		}else if (type == 2) {
			website = sys_web_service + "webview/parm/blogaftersale_"+path;
		}else if (type == 4) {
			webview.setVisibility(View.GONE);
			ll_coupons_info.setVisibility(View.VISIBLE);
		}else if (type == 5) {
			website = "http://m.kuaidi100.com/";
		}else if (type == 6 || type == 12) {//根据传过来的链接，直接访问网页
			website = path;
            if (website.startsWith("www")) {
                website = "http://"+website;
            }
		}else if (type == 7) {
			website = sys_web_service + "webview/parm/indexAdDetail_"+path;
		}else if (type == 8) {
			website = sys_web_service + "webview/parm/modelAdDetail_"+path;
		}else if (type == 9) {
			website = sys_web_service + "webview/parm/yijiaAdDetail_"+path;
		}else if (type == 15) {
			website = sys_web_service + "webview/parm/guidedetail_"+path;
		}else if (type == 16) {
			website = sys_web_service + "webview/parm/blogtypeAdDetail_"+path;
		}
		webview.loadUrl(website);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_left_btn:
			finish();
			break;
		}
	}

	@Override
	protected void callBeforeDataBack(HemaNetTask netTask) {

	}

	@Override
	protected void callAfterDataBack(HemaNetTask netTask) {

	}

	@Override
	protected void callBackForServerSuccess(HemaNetTask netTask,
			HemaBaseResult baseResult) {

	}

	@Override
	protected void callBackForServerFailed(HemaNetTask netTask,
			HemaBaseResult baseResult) {

	}

	@Override
	protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {

	}

	@Override
	protected void findView() {
		left = (ImageButton) findViewById(R.id.title_left_btn);
		title = (TextView) findViewById(R.id.title_text);
		right = (Button) findViewById(R.id.title_right_btn);
		webview = (WebView) findViewById(R.id.webview);
		progressBar = (ProgressBar) findViewById(R.id.progressBar);
		ll_coupons_info = (LinearLayout) findViewById(R.id.ll_coupons_info);
	}

	@Override
	protected void getExras() {
		path = mIntent.getStringExtra("parm");
		type = mIntent.getIntExtra("type", 0);
	}

	@Override
	protected void setListener() {
		left.setOnClickListener(this);
		if (type == 1) {
			title.setText("质量保证");
		}else if (type == 2) {
			title.setText("退货说明");
		}else if (type == 3) {
			title.setText("关于易家农户");
		}else if (type == 4) {
			title.setText("返利说明");
		}else if (type == 5) {
			title.setText("物流查询");
		}else if (type == 6) {
			title.setText("邀请注册");
		}else if (type == 7 || type == 8 || type == 9 || type == 12 || type == 16) {
			title.setText("图文详情");
		}else if (type == 10) {
			title.setText("众农");
		}else if (type == 11) {
			title.setText("等级说明");
		}else if (type == 13) {
			title.setText("申请说明");
		}else if (type == 14) {
			title.setText("田家介绍");
		}else if (type == 15) {
			title.setText("新手指南");
		}else if (type == 17) {
            title.setText("利率说明");
        }
		right.setVisibility(View.GONE);
		webview.setWebChromeClient(new WebChromeClient() {
			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				super.onProgressChanged(view, newProgress);
				if (newProgress == 100) {
					progressBar.setVisibility(View.GONE);
				}else {
					progressBar.setVisibility(View.VISIBLE);
					progressBar.setProgress(newProgress);
				}
			}
		});
	}
	
	/**初始化webview
	 * 
	 */
	private void initWebView() {
		// 支持javascript
		webview.getSettings().setJavaScriptEnabled(true);
		// 设置可以支持缩放
		webview.getSettings().setSupportZoom(true);
		// 设置出现缩放工具
		webview.getSettings().setBuiltInZoomControls(false);
		// 扩大比例的缩放
		webview.getSettings().setUseWideViewPort(true);
		// 自适应屏幕
		webview.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
		webview.getSettings().setLoadWithOverviewMode(true);
		// 取消显示滚动条
		webview.setVerticalScrollBarEnabled(false);
		webview.setHorizontalScrollBarEnabled(false);
		webview.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true;
			}
		});

        webview.getSettings().setSavePassword(true);
        webview.getSettings().setSaveFormData(true);
        webview.getSettings().setGeolocationEnabled(true);
        webview.getSettings().setGeolocationDatabasePath("/data/data/org.itri.html5webview/databases/");     // enable Web Storage: localStorage, sessionStorage
        webview.getSettings().setDomStorageEnabled(true);
        webview.requestFocus();
//        webview.setScrollBarStyle(0);
	}

    @Override
    protected boolean onKeyBack() {
        if (webview.canGoBack()) {
            webview.goBack();// 返回前一个页面
            return true;
        }
        return super.onKeyBack();
    }
}
